package com.github.xuemingyu.springboot.jkfaststart.controller.admin;

import com.bidanet.bdcms.core.bean.AjaxCallBack;
import com.bidanet.bdcms.core.vo.Page;
import com.bidanet.springmvc.demo.jkbuilder.JkFormBuilder;
import com.bidanet.springmvc.demo.jkbuilder.JkTableBuilder;
import com.github.xuemingyu.springboot.jkfaststart.JkConfig;
import com.github.xuemingyu.springboot.jkfaststart.common.AjaxPage;
import com.github.xuemingyu.springboot.jkfaststart.controller.admin.view.user.AddAdminUser;
import com.github.xuemingyu.springboot.jkfaststart.controller.admin.view.user.AdminUserHeader;
import com.github.xuemingyu.springboot.jkfaststart.controller.admin.view.user.AdminUserTable;
import com.github.xuemingyu.springboot.jkfaststart.controller.admin.view.user.AdminUserTableConverter;
import com.github.xuemingyu.springboot.jkfaststart.domain.AdminUser;
import com.github.xuemingyu.springboot.jkfaststart.domain.type.Status;
import com.github.xuemingyu.springboot.jkfaststart.service.AdminUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

@Controller
@RequestMapping(JkConfig.ADMIN_PATH+"/admin")
public class AdminUserController {

    @Autowired
    AdminUserService adminUserService;

    @RequestMapping("/index")
    public String index(Model model){

        return JkTableBuilder.create(AdminUserTable.class)
                .addHeaderTool(new AdminUserHeader()).build(model);
    }

    @RequestMapping("/add")
    public String add(AddAdminUser addAdminUser, Model model){
        return JkFormBuilder.create(addAdminUser).build(model);
    }

    @RequestMapping("add_save")
    @ResponseBody
    public AjaxCallBack addSave(AddAdminUser addAdminUser){
        adminUserService.saveUser(addAdminUser);
        return AjaxCallBack.saveSuccess();
    }

    @RequestMapping("/edit")
    public String edit(Long id, Model model){
        AdminUser user = adminUserService.get(id);
        AddAdminUser addAdminUser = new AddAdminUser();
        BeanUtils.copyProperties(user,addAdminUser);
        return JkFormBuilder.create(addAdminUser).build(model);
    }
    @RequestMapping("/data")
    @ResponseBody
    public AjaxPage<AdminUserTable> data(AdminUser adminUser, Page<AdminUser> page){
        adminUserService.eqPage(adminUser,page);
        Page<AdminUserTable> tablePage = new Page<>();
        tablePage.setList(new ArrayList<>());
        tablePage.setTotal(page.getTotal());
        tablePage.setPageSize(page.getPageSize());
        tablePage.setPageCurrent(page.getPageCurrent());

        for (AdminUser user : page.getList()) {
            AdminUserTable userTable = AdminUserTableConverter.convertToAdminUserTable(user);
            tablePage.getList().add(userTable);
        }

        return AjaxPage.success(tablePage);

    }

    @RequestMapping("/changeStatus")
    @ResponseBody
    public AjaxCallBack changeStatus(Long id ){
      AdminUser adminUser= adminUserService.get(id);
      if(adminUser.getStatus()== Status.open){
          adminUser.setStatus(Status.close);
      }else {
          adminUser.setStatus(Status.open);
      }
        adminUserService.save(adminUser);
        return AjaxCallBack.handleSuccess();
    }


    @RequestMapping("/delete")
    @ResponseBody
    public AjaxCallBack delete(Long id ){
        AdminUser adminUser= adminUserService.get(id);
       adminUser.setDelete(true);
        adminUserService.save(adminUser);
        return AjaxCallBack.handleSuccess();
    }
}
